import java.util.Arrays;
import java.util.Random;
public class BubbleSort
{
	public static void main(String[] args){
		int[] elements = generateNum(20);
		int[] elementsAfterSort = bubbleSort(elements);

		System.out.println(Arrays.toString(elementsAfterSort));
	}

	public static int[] bubbleSort(int[] elements){
		
		boolean isChange = false ;

		for(int i = 0;i < elements.length - 1 ; i ++){
			isChange = false ;
			for(int j = 0 ; j < elements.length - i - 1; j ++){
				
				if(elements[j] > elements[j + 1]){
					
					elements[j] = elements[j] + elements[j + 1];
					elements[j + 1] = elements[j] - elements[j + 1];
					elements[j] = elements[j] - elements[j + 1];
					
					isChange = true ;

				}
			}

			if(!isChange){
				break ;
			}
		}

		return elements ;
	}

	public static int[] generateNum(long numCount){
		
		Random random = new Random();
		int[] result = new int[(int)numCount];
		
		for(int i = 0 ; i < numCount ; i ++){
			result[i] = random.nextInt(500);	
		}
		return result;
	}
}